﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace IssueIT.Core.Domain.Model
{
	public class TagList : IEnumerable<Tag>
	{
		private readonly ICollection<Tag> _tagList = new HashSet<Tag>();

		public int Count
		{
			get
			{
				return _tagList.Count;
			}
		}

		public void Add(Tag tag)
		{
			if( Contains(tag) )
			{
				return;
			}
			_tagList.Add(tag);
		}

		public bool Contains(Tag tag)
		{
			return Contains(tag.Text);
		}

		public bool Contains(string text)
		{
			return findByText(text)!=null;
		}

		private Tag findByText(string text)
		{
			return _tagList.FirstOrDefault(tag => tag.Text.Equals(text, StringComparison.OrdinalIgnoreCase));
		}

		public bool Remove(Tag tag)
		{
			return _tagList.Remove(tag);
		}

		public bool Remove(string text)
		{
			var tag = findByText(text);
			return Remove(tag);
		}

		public void Clear()
		{
			_tagList.Clear();
		}

		public IEnumerator<Tag> GetEnumerator()
		{
			return _tagList.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}
	}
}